package com.tpay.client.presenter;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.NoGatekeeper;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.Place;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;
import com.tpay.client.CurrentUser;
import com.tpay.client.G;
import com.tpay.client.NameTokens;
import com.tpay.client.event.LoginAuthenticatedEvent;
import com.tpay.client.uihandlers.LoginPageUiHandlers;
import com.tpay.shared.action.LoginAction;
import com.tpay.shared.action.LoginResult;

public class LoginPagePresenter extends Presenter<LoginPagePresenter.MyView, LoginPagePresenter.MyProxy> implements LoginPageUiHandlers {
	@ProxyStandard
	@NameToken(NameTokens.loginPage)
	@NoGatekeeper
	public interface MyProxy extends Proxy<LoginPagePresenter>, Place {
	}
	
	public interface MyView extends View, HasUiHandlers<LoginPageUiHandlers> {
		String getId();
		String getPassword();
		void resetAndFocus();
		void say(Throwable e);
	}

	public static final String REDIRECT = "redirect";
	
	private final PlaceManager placeManager;
	private final DispatchAsync dispatcher;
	
	private String redirectToken;
	
	@Inject
	public LoginPagePresenter(EventBus eventBus, MyView view, MyProxy proxy, DispatchAsync dispatcher, PlaceManager placeManager) {
		super(eventBus, view, proxy);

		this.dispatcher = dispatcher;
		this.placeManager = placeManager;
		
		this.getView().setUiHandlers(this);
	}

	@Override
	protected void revealInParent() {
		RevealRootContentEvent.fire(this, this);
	}

	@Override
	public void prepareFromRequest(PlaceRequest request) {
		super.prepareFromRequest(request);
		
		redirectToken = request.getParameter(REDIRECT, "");
	}

	@Override
	protected void onReset() {
		super.onReset();
		
		getView().resetAndFocus();
	}

	@Override
	public void onLoginButtonClicked() {
		String id = getView().getId();
		String password = getView().getPassword();
		
		dispatcher.execute(new LoginAction(id, password), new AsyncCallback<LoginResult> () {

			@Override
			public void onFailure(Throwable caught) {
				G.ShowMessage(caught);
				getView().resetAndFocus();
			}

			@Override
			public void onSuccess(LoginResult result) {
				CurrentUser currentUser = new CurrentUser(getView().getId());
				
				LoginAuthenticatedEvent.fire(getEventBus(), currentUser);
				
				PlaceRequest request = new PlaceRequest(NameTokens.mainPage);
				if(!redirectToken.isEmpty()) 
					request = request.with(REDIRECT, redirectToken);
				
				placeManager.revealPlace(request);
			}
		});
		
	} 
}
